package ui.custom.table;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import type.ITableLabelProviderObject;

public class CheckTableViewerWrapper {
	private TableViewer tv;
	private CheckboxTableViewer ctv;
	private List<ITableLabelProviderObject> data;
	private List<ITableLabelProviderObject> initData;
	public CheckTableViewerWrapper(Composite parent,List<ITableLabelProviderObject> list){
		this.data = list;
		this.initData = new ArrayList<ITableLabelProviderObject>();
		initData.addAll(list);
		tv = new TableViewer(parent,SWT.CHECK|SWT.FULL_SELECTION);
		init();
		ctv  = new CheckboxTableViewer(tv.getTable());
	}
	private void init() {
		tv.setContentProvider(new ContentProvider());
		tv.setLabelProvider(new LabelProvider());
		tv.setInput(data);
		// Set up the table
		Table table = tv.getTable();
		ITableLabelProviderObject obj = data.get(0);
		for(int i=0;i<obj.getColumnCount();i++){
			TableColumn column = new TableColumn(table, SWT.CENTER);
			column.setText(obj.getColumnTitle(i));
			column.setWidth(obj.getColumnWidth(i));
		}
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		// Create the cell editors
		CellEditor[] editors = new CellEditor[obj.getColumnCount()];
		for(int i=0;i<obj.getColumnCount();i++){
			editors[i] = obj.getEditorByColumnIndex(i,table);
		}
		Listener paintListener = new Listener() {
            public void handleEvent(Event event) {
                switch(event.type) {        
                    case SWT.MeasureItem: {
                        TableItem item = (TableItem)event.item;
                        String text = getText(item, event.index);
                        Point size = event.gc.textExtent(text);
                        event.width = size.x;
                        event.height = Math.max(event.height, size.y);
                        break;
                    }
                    case SWT.PaintItem: {
                        TableItem item = (TableItem)event.item;
                        String text = getText(item, event.index);
                        Point size = event.gc.textExtent(text);                    
                        int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
                        event.gc.drawText(text, event.x, event.y + offset2, true);
                        break;
                    }
                    case SWT.EraseItem: {    
                        event.detail &= ~SWT.FOREGROUND;
                        break;
                    }
                }
            }
            String getText(TableItem item, int column) {
            	return item.getText(column);
            }
        };
       table.addListener(SWT.MeasureItem, paintListener);
       table.addListener(SWT.PaintItem, paintListener);
       table.addListener(SWT.EraseItem, paintListener);

		String[] properties = new String[obj.getColumnCount()];
		for(int i=0;i<obj.getColumnCount();i++){
			properties[i] = obj.getNameByIndex(i);
		}
		// Set the editors, cell modifier, and column properties
		tv.setColumnProperties(properties);
		tv.setCellModifier(new CellModifier(tv));
		tv.setCellEditors(editors);
		tv.refresh();
	}
	public TableViewer getViewer() {
		return tv;
	}
	public Object[] getCheckedItem(){
		return ctv.getCheckedElements();
	}
	public List<ITableLabelProviderObject> getData() {
		return data;
	}
	public List<ITableLabelProviderObject> getInitData() {
		return initData;
	}
}
